/*
* @Author: Karry
* @Date:   2017-10-29 16:33:46
* @Last Modified by:   Karry
* @Last Modified time: 2018-01-04 16:14:20
*/

require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var _db           = require('util/db.js');
var _product      = require('service/product-service.js');
var _cart         = require('service/cart-service.js');
var templateIndex = require('./index.string');

var page = {
    data : {
        productId  :  _db.getUrlParam('productId') || '',
    },
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        // 如果没有传productId，自动调回首页
        if (!this.data.productId) {
            _db.goHome();
        }
        this.loadDetail();
    },
    bindEvent : function(){
        var _this = this;
        // 图片预览
        $(document).on('mouseenter', '.p-img-item',function(){
            var imageUrl = $(this).find('.p-img').attr('src');
            $('.main-img').attr('src',imageUrl);
        });
        // count的操作
        $(document).on('click', '.p-count-btn',function(){
            var type      = $(this).hasClass('plus') ? 'plus' : 'minus',
                $pCount   = $('.p-count'),
                currCount = parseInt($pCount.val()),
                minCount  = 1,
                maxCount  = _this.data.detailInfo.stock || 1;
            if (type === 'plus') {
                $pCount.val(currCount < maxCount ? currCount + 1 : maxCount);
            }
            else if (type === 'minus') {
                $pCount.val(currCount > minCount ? currCount - 1 : minCount);
            }
        });
        // 加入购物车
        $(document).on('click', '.cart-add', function(){
            _cart.addToCart({
                productId : _this.data.productId,
                count     : $('.p-count').val()
            }, function(res){
                window.location.href = './result.html?type=cart-add';
            }, function(errMsg){
                _db.errorTips(errMsg);
            });
        });
        // 立刻购买
        $(document).on('click', '.immediately-buy', function(){
            _cart.addToCart({
                productId : _this.data.productId,
                count     : $('.p-count').val()
            }, function(res){
                window.location.href = './order-confirm.html';
            }, function(errMsg){
                _db.errorTips(errMsg);
            });
        });
    },
    // 加载商品详情数据
    loadDetail : function(){
       var  _this       = this,
            html        = '',
            $pageWrap   = $('.page-wrap');
       // loading
       $pageWrap.html('<div class="loading"></div>');
       // 请求detail信息
       _product.getProductDetail(this.data.productId, function(res){
            _this.filter(res);
            // 缓存住detail的数据
            _this.data.detailInfo = res;
            // 渲染HTML render
            html = _db.renderHtml(templateIndex, res);
            $pageWrap.html(html);
       }, function(errMsg){
            $pageWrap.html('<p class="err-tip">亲，你找的商品太淘气啦，我们正在努力寻找中...</p>');
       });
    },
    // 数据匹配
    filter : function(data){
        data.subImages = data.subImages.split(',');
    }
};
$(function(){
    page.init();
})